﻿Public Class FrmAgences
    Private szSQL As String
    Private rs As ADODB.Recordset

    Private Sub FrmAgences_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

        ' largeur form réduite
        Me.Width = 385
        InitListeAgences(0)
        ' utilisateur autorisé à modifier ?
        If gUserAcces < 9 Then
            BO_Enregistrer.Enabled = False
        Else
            BO_Enregistrer.Enabled = True
        End If

    End Sub

    Private Sub InitListeAgences(ByVal ix As Integer)

        Dim nb As Integer = 0

        ' effacer la listview
        LV_AGENCES.Items.Clear()

        ' Lire les AGENCES
        szSQL = "CALL spGet_P_TABLES_byCODE('P_AGENCES',0)"
        GetRecordset(ctDSN_AEM, szSQL, rs)

        ' Load the data.
        Do While Not rs.EOF
            LV_AGENCES.Items.Add(rs.Fields("code").Value)
            LV_AGENCES.Items(nb).SubItems.Add(rs.Fields("libelle").Value)
            LV_AGENCES.Items(nb).SubItems.Add(nv(rs.Fields("ville").Value))
            ' Get the next record.
            rs.MoveNext()
            nb = nb + 1
        Loop
        ' Close the recordset AND connection.
        rs.Close()

        If nb > 0 Then
            LB_NbLignes.Text = CStr(nb) & " lignes"
        End If
        ' restaurer la position du curseur avant le init
        setIndice(ix, LV_AGENCES)

    End Sub

    Private Sub LV_AGENCES_DoubleClick(ByVal sender As Object, ByVal e As System.EventArgs) Handles LV_AGENCES.DoubleClick
        If LV_AGENCES.SelectedItems.Count > 0 Then
            AgencetoScreen(LV_AGENCES.SelectedItems(0).Text)
            ' afficher les détails de la formule
            TC_Details.Visible = True
            TE_NOM.Focus()
            ' largeur form agrandi
            Me.Width = 746
        End If
    End Sub

    Private Sub LV_AGENCES_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles LV_AGENCES.KeyDown
        Select Case e.KeyCode
            ' touche Enter = DoubleClick
            Case Keys.Enter
                LV_AGENCES_DoubleClick(sender, e)

            Case Keys.Insert
                BO_ADD_LIGNE_Click(sender, e)

        End Select

    End Sub

    Private Sub AgencetoScreen(ByVal ref As String)

        ' Lire l'enreg sélectionné
        szSQL = "CALL spGet_P_TABLES_byCODE('P_AGENCES'," & ref & ")"
        GetRecordset(ctDSN_AEM, szSQL, rs)
        If rs.RecordCount > 0 Then
            TE_CODE.Text = rs.Fields("code").Value
            TE_NOM.Text = rs.Fields("libelle").Value
            TE_EMAIL.Text = nv(rs.Fields("email").Value)
            TE_ADRESSE.Text = nv(rs.Fields("adresse").Value)
            TE_CPOSTAL.Text = nv(rs.Fields("codepostal").Value)
            TE_VILLE.Text = nv(rs.Fields("ville").Value)
            TE_CENTRE.Text = nv(rs.Fields("centre").Value)
            TE_TEL.Text = nv(rs.Fields("telephone").Value)
            TE_JNL_BNQ.Text = nv(rs.Fields("jnl_bnq").Value)
            TE_CPT_BNQ.Text = rs.Fields("cpt_bnq").Value
            TE_DELAI.Value = rs.Fields("delai_annul").Value
            TE_DERN_VALIDATION.Text = IfDate(rs.Fields("dern_validation").Value)
            TE_DERN_EXPORTATION.Text = IfDate(rs.Fields("dern_export").Value)

        End If
        rs.Close()

    End Sub

    Private Sub BO_Enregistrer_Click_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BO_Enregistrer.Click

        ' mise à jour 
        szSQL = "CALL spUpd_AGENCES(" & TE_CODE.Text & ",'" & nq(TE_NOM.Text) & "','" & TE_CENTRE.Text & "'," & TE_DELAI.Text & ",'" & _
            TE_TEL.Text & "','" & TE_ADRESSE.Text & "','" & TE_CPOSTAL.Text & "','" & TE_VILLE.Text & "','" & TE_EMAIL.Text & "','" & _
            TE_JNL_BNQ.Text & "'," & TE_CPT_BNQ.Text & ")"
        ExecQuery(ctDSN_AEM, szSQL)
        ' Cacher les détails
        BO_ANNULER_Click(sender, e)

    End Sub

    Private Sub BO_ANNULER_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BO_ANNULER.Click
        ' largeur form réduite
        Me.Width = 386
        ' Cacher les détails
        TC_Details.Visible = False
        ' réaffiche la viewlist
        InitListeAgences(LV_AGENCES.SelectedItems(0).Index)
    End Sub

    Private Sub BO_ADD_LIGNE_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BO_ADD_LIGNE.Click
        MsgBox("Veuillez utiliser l'utilitaire MySQL Workbench pour créer une nouvelle agence", MsgBoxStyle.Information)

    End Sub
End Class